/*************************************************
 *FileName:      agency.js
 *Description:   申请代理.js
 *Others:        陈灿
 *************************************************/
    /*
     * 启动
     */
    mui.init();
    mui('.mui-scroll-wrapper').scroll({
        indicators: false,      //是否显示滚动条 默认为true
        deceleration: 0.003,    //阻尼系数,系数越小滑动越灵敏 默认0.0006
        bounce: false           //是否启用回弹 默认true
    });
    mui.init({
        beforeback: function() {
            var list = plus.webview.currentWebview().opener();
            mui.fire(list, 'refreshindex');
            return true;
        }
    });
    /*
     * 变量初始化
     */
    var that = getUrlParam('that');
    var token = localStorage.replace_token;
    var uid = localStorage.replace_uid;
    var activity_province , activity_city ,activity_district ;
    var status='';
    /*
     * 主程序
     */ 
    $(document).ready(function() 
    {
        //样式加载
        pagecss();
        //状态
//      state();
    });
    mui.plusReady(state)
    /**
     * pagecss
     * 样式加载
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function pagecss(){
        $('.useragrre').on('tap',function(){
            $(this).toggleClass('active');
        })
        
        $('.useragrre-a').on('tap',function(event){
            common.pageTransition('useragrre.html?that=agency')
            return false;
        })
        
        var _getParam = function(obj, param) {return obj[param] || '';};
        var cityPicker3 = new mui.PopPicker({layer: 3});
        cityPicker3.setData(cityData3);
        $('.agency').on('focusin','.range2', function(e) {
            $(this).blur();
            cityPicker3.show(function(items) {
                activity_province = _getParam(items[0], 'text');
                activity_city = _getParam(items[1], 'text');
                activity_district = _getParam(items[2], 'text');
                $('.range').val(activity_province+'-'+activity_city+'-'+activity_district);
            });
        })
    }
    
    //状态获取
    function state(){
        $.ajax({
            type: "post",
            url: DOMAIN_NAME + "/index.php/api/users_verify/UsersVerify",
            dataType:'jsonp',
            data: { uid: uid, token: token,type : 4},
            success: function(data) {
                if(data.error_code == 0){
                    status =data.data.status;
                    if(status!=2){
                        if(status!=3&&status!=2){
                            $('.personal input').attr('readonly','readonly');
                            $('.range').removeClass('range2');
                        }
                        $('.name').val(data.data.name);
                        $('.phone').val(data.data.phone);
                        $('.wx').val(data.data.wx);
                        $('.range').val(data.data.activity_province+'-'+data.data.activity_city+'-'+data.data.activity_district);
                        if(status==1||status=='0'){
                            $('.rdntn').addClass('rmcolor');
                        }
                    }
                }else if(data.error_code == 1001){
                    setTimeout(function(){
                        exitlogin();
                    },1000);
                }else{
                }
            }  
        });
    }
    
    //提交
    $('.rdntn').on('tap',function(){
        var oname = $('.name').val();
        var ophone = $('.phone').val();
        var owx = $('.wx').val();
        var orange = $('.range').val();
        if(status==1||status=="0"){
            return false; 
        }else if(!orange){
            mui.toast('请选择代理区域！');
            return false; 
        }else if(!oname){
            mui.toast('请输入真实姓名！');
            return false; 
        }else if(!(/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(ophone) ) ){ //判断手机号
            mui.toast('请输入正确手机号码！');
            return false; 
        }else if(!owx){
            mui.toast('请输入微信号！');
            return false; 
        }else if(!$('.useragrre').hasClass('active')  ){
            mui.toast('请同意协议！');
        }else{
            var task = plus.uploader.createUpload(DOMAIN_NAME + "/index.php/api/users_verify/verifyadd",
            { method:"POST",blocksize:20480,priority:100 },
                function (t, status) { //上传完成
                    common.loadingOut();
                    if (status == 200) {
                        mui.confirm('代理申请已提交平台，请等待审核！','提示',['确定','取消'],function(e){ 
                            if(e.index==0){
                                setTimeout(function(){mui.back()},500);
                            }
                        },'div');
                    }else{ 
                        mui.toast('上传失败',{ duration:2000, type:'div' });
                    }
                }
            );
            common.loadingIn();
            task.addData("name", oname);
            task.addData("uid", uid);
            task.addData("token",token);
            task.addData("phone", ophone);
            task.addData("wx", owx);
            task.addData("activity_province", activity_province);
            task.addData("activity_city", activity_city);
            task.addData("activity_district", activity_district);
            task.addData("type", "4");
            mui.toast('开始上传',{ duration:2000, type:'div' });
            task.start();
        }
    }) 
            
    /**
     * adds //方法名
     * @陈灿 //作者名
     * @DateTime 2018-01-010T0116:25:00+0800 //时间
     * @param    int    $goods_id //参数
     * @param    array  $data //参数
     * @return    array  [error_code, error_msg, goods_id] //返回值
     */